home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dist / java3d.idb / usr / demos / java / j3d / programs / examples / GeometryByReference / InterleavedTest.java.z / InterleavedTest.java
Encoding:
Java Source  |  2003-08-08  |  14.7 KB  |  500 lines

  1. /*
  2.  *    @(#)InterleavedTest.java 1.13 02/10/21 13:41:02
  3.  *
  4.  * Copyright (c) 1996-2002 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  *
  10.  * - Redistributions of source code must retain the above copyright
  11.  *   notice, this list of conditions and the following disclaimer.
  12.  *
  13.  * - Redistribution in binary form must reproduce the above copyright
  14.  *   notice, this list of conditions and the following disclaimer in
  15.  *   the documentation and/or other materials provided with the
  16.  *   distribution.
  17.  *
  18.  * Neither the name of Sun Microsystems, Inc. or the names of
  19.  * contributors may be used to endorse or promote products derived
  20.  * from this software without specific prior written permission.
  21.  *
  22.  * This software is provided "AS IS," without a warranty of any
  23.  * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
  24.  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
  25.  * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
  26.  * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
  27.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  28.  * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
  29.  * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
  30.  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
  31.  * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
  32.  * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
  33.  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  34.  *
  35.  * You acknowledge that Software is not designed,licensed or intended
  36.  * for use in the design, construction, operation or maintenance of
  37.  * any nuclear facility.
  38.  */
  39.  
  40. import java.applet.Applet;
  41. import java.awt.*;
  42. import java.awt.event.*;
  43. import com.sun.j3d.utils.applet.MainFrame;
  44. import com.sun.j3d.utils.geometry.*;
  45. import com.sun.j3d.utils.universe.*;
  46. import com.sun.j3d.utils.image.TextureLoader;
  47. import javax.media.j3d.*;
  48. import javax.vecmath.*;
  49. import javax.swing.*;
  50. import javax.swing.event.*;
  51. import javax.swing.border.*;
  52. import com.sun.j3d.utils.behaviors.vp.*;
  53.  
  54. public class InterleavedTest extends JApplet implements ActionListener {
  55.  
  56.     RenderingAttributes ra;
  57.     ColoringAttributes ca;
  58.     Material mat;               
  59.     Appearance app;               
  60.     JComboBox geomType;
  61.     JCheckBox transparency;
  62.     JCheckBox textureBox;
  63.     Shape3D shape;               
  64.     TransparencyAttributes transp;
  65.  
  66.     GeometryArray tetraRegular, tetraStrip, tetraIndexed, tetraIndexedStrip;
  67.     GeometryArray[] geoArrays = new GeometryArray[4];
  68.  
  69.    // Globally used colors
  70.    Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
  71.    Color3f red = new Color3f(1.0f, 0.0f, 0.0f);
  72.    Color3f green = new Color3f(0.0f, 1.0f, 0.0f);
  73.    Color3f blue = new Color3f(0.0f, 0.0f, 1.0f);
  74.    Color3f[] colors = {white, red, green, blue};               
  75.  
  76.     private static final float sqrt3 = (float) Math.sqrt(3.0);
  77.     private static final float sqrt3_3 = sqrt3 / 3.0f;
  78.     private static final float sqrt24_3 = (float) Math.sqrt(24.0) / 3.0f;
  79.  
  80.     private static final float ycenter = 0.5f * sqrt24_3;
  81.     private static final float zcenter = -sqrt3_3;
  82.  
  83.     private static final Point3f p1 = 
  84.     new Point3f(-1.0f, -ycenter, -zcenter);
  85.     private static final Point3f p2 = 
  86.     new Point3f(1.0f, -ycenter, -zcenter);
  87.     private static final Point3f p3 =
  88.     new Point3f(0.0f, -ycenter, -sqrt3 - zcenter);
  89.     private static final Point3f p4 =
  90.     new Point3f(0.0f, sqrt24_3 - ycenter, 0.0f);
  91.  
  92.     private static final Point2f t1 = new Point2f(0.0f, 0.0f);
  93.     private static final Point2f t2 = new Point2f(0.5f, 1.0f);
  94.     private static final Point2f t3 = new Point2f(1.0f, 0.0f);
  95.     private static final Point2f t4 = new Point2f(1.0f, 1.0f);
  96.  
  97.     private static final Color3f c1 = new Color3f(1.0f, 0.0f, 0.0f);
  98.     private static final Color3f c2 = new Color3f(0.0f, 1.0f, 0.0f);
  99.     private static final Color3f c3 = new Color3f(0.0f, 1.0f, 1.0f);
  100.     private static final Color3f c4 = new Color3f(1.0f, 1.0f, 0.0f);
  101.     
  102.  
  103.     private static final float[] interleaved = {
  104.     t1.x, t1.y,
  105.     t1.x, t1.y,
  106.     c1.x, c1.y, c1.z, // front face
  107.     p1.x, p1.y, p1.z, // front face
  108.     t2.x, t2.y,
  109.     t2.x, t2.y,
  110.     c2.x, c2.y, c2.z,
  111.     p2.x, p2.y, p2.z,
  112.     t4.x, t4.y,
  113.     t4.x, t4.y,
  114.     c4.x, c4.y, c4.z,
  115.     p4.x, p4.y, p4.z,
  116.     
  117.     t1.x, t1.y,
  118.     t1.x, t1.y,
  119.     c1.x, c1.y, c1.z,// left, back face
  120.     p1.x, p1.y, p1.z,// left, back face
  121.     t4.x, t4.y,
  122.     t4.x, t4.y,
  123.     c4.x, c4.y, c4.z,
  124.     p4.x, p4.y, p4.z,
  125.     t3.x, t3.y,
  126.     t3.x, t3.y,
  127.     c3.x, c3.y, c3.z,
  128.     p3.x, p3.y, p3.z,
  129.     
  130.     t2.x, t2.y,
  131.     t2.x, t2.y,
  132.     c2.x, c2.y, c2.z,// right, back face
  133.     p2.x, p2.y, p2.z,// right, back face
  134.     t3.x, t3.y,
  135.     t3.x, t3.y,
  136.     c3.x, c3.y, c3.z,
  137.     p3.x, p3.y, p3.z,
  138.     t4.x, t4.y,
  139.     t4.x, t4.y,
  140.     c4.x, c4.y, c4.z,
  141.     p4.x, p4.y, p4.z,
  142.     
  143.     t1.x, t1.y,
  144.     t1.x, t1.y,
  145.     c1.x, c1.y, c1.z,// bottom face
  146.     p1.x, p1.y, p1.z,// bottom face
  147.     t3.x, t3.y,
  148.     t3.x, t3.y,
  149.     c3.x, c3.y, c3.z,
  150.     p3.x, p3.y, p3.z,
  151.     t2.x, t2.y,
  152.     t2.x, t2.y,
  153.     c2.x, c2.y, c2.z,
  154.     p2.x, p2.y, p2.z,
  155.     };
  156.  
  157.     private static final float[] indexedInterleaved = {
  158.     t1.x,t1.y,
  159.     t1.x,t1.y,
  160.     c1.x,c1.y,c1.z,
  161.     p1.x,p1.y,p1.z,
  162.     t2.x,t2.y,
  163.     t2.x,t2.y,
  164.     c2.x,c2.y,c2.z,
  165.     p2.x,p2.y,p2.z,
  166.     t3.x,t3.y,
  167.     t3.x,t3.y,
  168.     c3.x,c3.y,c3.z,
  169.     p3.x,p3.y,p3.z,
  170.     t4.x,t4.y,
  171.     t4.x,t4.y,
  172.     c4.x,c4.y,c4.z,
  173.     p4.x,p4.y,p4.z,
  174.     };
  175.     
  176.  
  177.     private static final int[] indices = {0,1,3,0,3,2,1,2,3,0,2,1};
  178.     private int[] stripVertexCounts = {3,3,3,3};
  179.  
  180.     TextureUnitState textureUnitState[] = new TextureUnitState[2];
  181.     Texture tex1;
  182.     Texture tex2;
  183.  
  184.     private java.net.URL texImage1 = null;
  185.     private java.net.URL texImage2 = null;
  186.  
  187.     private SimpleUniverse u;
  188.  
  189.    BranchGroup createSceneGraph() {
  190.     BranchGroup objRoot = new BranchGroup();
  191.  
  192.     // Set up attributes to render lines
  193.         app = new Appearance();
  194.     app.setCapability(Appearance.ALLOW_TEXTURE_UNIT_STATE_WRITE);
  195.     
  196.     transp = new TransparencyAttributes();
  197.     transp.setTransparency(0.5f);
  198.     transp.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
  199.     transp.setTransparencyMode(TransparencyAttributes.NONE);
  200.     app.setTransparencyAttributes(transp);
  201.  
  202.         // load textures
  203.         TextureAttributes texAttr1 = new TextureAttributes();
  204.         texAttr1.setTextureMode(TextureAttributes.DECAL);
  205.         TextureAttributes texAttr2 = new TextureAttributes();
  206.         texAttr2.setTextureMode(TextureAttributes.MODULATE);
  207.  
  208.         TextureLoader tex = new TextureLoader(texImage1, new String("RGB"), this);
  209.         if (tex == null)
  210.             return null;
  211.         tex1 = tex.getTexture();
  212.  
  213.         tex = new TextureLoader(texImage2, new String("RGB"), this);
  214.         if (tex == null)
  215.             return null;
  216.         tex2 = tex.getTexture();
  217.  
  218.         textureUnitState[0] = new TextureUnitState(tex1, texAttr1, null);
  219.         textureUnitState[1] = new TextureUnitState(tex2, texAttr2, null);
  220.  
  221.     tetraRegular = createGeometry(1);
  222.     tetraStrip =createGeometry(2);
  223.     tetraIndexed = createGeometry(3);
  224.     tetraIndexedStrip = createGeometry(4);
  225.  
  226.     geoArrays[0] = tetraRegular;
  227.     geoArrays[1] = tetraStrip;
  228.     geoArrays[2] = tetraIndexed;
  229.     geoArrays[3] = tetraIndexedStrip;
  230.     
  231.     shape = new Shape3D(tetraRegular, app);
  232.     shape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
  233.  
  234.     Transform3D t = new Transform3D();
  235.     // move the object upwards
  236.     t.set(new Vector3f(0.0f, 0.3f, 0.0f));
  237.  
  238.     // rotate the shape
  239.     Transform3D temp = new Transform3D();
  240.         temp.rotX(Math.PI/4.0d);
  241.     t.mul(temp);
  242.         temp.rotY(Math.PI/4.0d);
  243.         t.mul(temp);
  244.     
  245.     // Shrink the object 
  246.     t.setScale(0.6);
  247.  
  248.     TransformGroup trans = new TransformGroup(t);
  249.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  250.     trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
  251.  
  252.     objRoot.addChild(trans);
  253.     trans.addChild(shape);
  254.  
  255.     BoundingSphere bounds =
  256.         new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
  257.  
  258.     // Set up the global lights
  259.     Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
  260.     Vector3f lDir1  = new Vector3f(-1.0f, -1.0f, -1.0f);
  261.     Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
  262.  
  263.     AmbientLight aLgt = new AmbientLight(alColor);
  264.     aLgt.setInfluencingBounds(bounds);
  265.     DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
  266.     lgt1.setInfluencingBounds(bounds);
  267.     objRoot.addChild(aLgt);
  268.     objRoot.addChild(lgt1);
  269.     
  270.     // Let Java 3D perform optimizations on this scene graph.
  271.         objRoot.compile();
  272.  
  273.     return objRoot;
  274.     }
  275.  
  276.     JPanel createGeometryByReferencePanel() {
  277.     JPanel panel = new JPanel();
  278.     panel.setBorder(new TitledBorder("Geometry Type"));
  279.  
  280.     String values[] = {"Array", "Strip", "Indexed", "IndexedStrip"};
  281.     geomType = new JComboBox(values);
  282.     geomType.setLightWeightPopupEnabled(false);
  283.     geomType.addActionListener(this);
  284.     geomType.setSelectedIndex(0);
  285.     panel.add(new JLabel("Geometry Type"));     
  286.     panel.add(geomType);
  287.  
  288.     transparency = new JCheckBox("EnableTransparency", 
  289.                       false);
  290.     transparency.addActionListener(this);
  291.     panel.add(transparency);
  292.  
  293.     textureBox = new JCheckBox("EnableTexture", false);
  294.     textureBox.addActionListener(this);
  295.     panel.add(textureBox);
  296.  
  297.     return panel;
  298.     }
  299.  
  300.     public InterleavedTest() {
  301.     }
  302.  
  303.     public InterleavedTest(java.net.URL texURL1, java.net.URL texURL2) {
  304.     texImage1 = texURL1;
  305.     texImage2 = texURL2;
  306.     }
  307.  
  308.     public void init() {
  309.  
  310.     // create textures
  311.  
  312.         if (texImage1 == null) {
  313.           // the path to the image for an applet
  314.           try {
  315.             texImage1 = new java.net.URL(getCodeBase().toString() +
  316.                                       "../images/bg.jpg");
  317.           }
  318.           catch (java.net.MalformedURLException ex) {
  319.             System.out.println(ex.getMessage());
  320.             System.exit(1);
  321.           }
  322.         }
  323.  
  324.         if (texImage2 == null) {
  325.           // the path to the image for an applet
  326.           try {
  327.             texImage2 = new java.net.URL(getCodeBase().toString() +
  328.                                     "../images/one.jpg");
  329.           }
  330.           catch (java.net.MalformedURLException ex) {
  331.             System.out.println(ex.getMessage());
  332.             System.exit(1);
  333.           }
  334.         }
  335.  
  336.     Container contentPane = getContentPane();
  337.     
  338.         Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
  339.         contentPane.add("Center", c);
  340.  
  341.         BranchGroup scene = createSceneGraph();
  342.         // SimpleUniverse is a Convenience Utility class
  343.         u = new SimpleUniverse(c);
  344.  
  345.     // add mouse behaviors to the viewingPlatform
  346.     ViewingPlatform viewingPlatform = u.getViewingPlatform();
  347.  
  348.         // This will move the ViewPlatform back a bit so the
  349.         // objects in the scene can be viewed.
  350.         viewingPlatform.setNominalViewingTransform();
  351.  
  352.     // add Orbit behavior to the viewing platform
  353.     OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL);
  354.     BoundingSphere bounds =
  355.         new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
  356.     orbit.setSchedulingBounds(bounds);
  357.     viewingPlatform.setViewPlatformBehavior(orbit);
  358.     
  359.         u.addBranchGraph(scene);
  360.  
  361.  
  362.     // Create GUI
  363.     JPanel p = new JPanel();
  364.     BoxLayout boxlayout = new BoxLayout(p, 
  365.                         BoxLayout.Y_AXIS);
  366.     p.add(createGeometryByReferencePanel());
  367.     p.setLayout(boxlayout);
  368.  
  369.     contentPane.add("South", p);
  370.     }
  371.  
  372.     public void destroy() {
  373.     u.cleanup();
  374.     }
  375.  
  376.     public void actionPerformed(ActionEvent e) {
  377.     Object target = e.getSource();
  378.     if (target == geomType) {
  379.         shape.setGeometry(geoArrays[geomType.getSelectedIndex()]);
  380.  
  381.     } 
  382.     else if (target == transparency) {
  383.         if (transparency.isSelected()) {
  384.         transp.setTransparencyMode(TransparencyAttributes.BLENDED);
  385.         }
  386.         else {
  387.         transp.setTransparencyMode(TransparencyAttributes.NONE);
  388.         }
  389.     } 
  390.     else if (target == textureBox) {
  391.         if (textureBox.isSelected()) {
  392.         app.setTextureUnitState(textureUnitState);
  393.         }
  394.         else {
  395.         app.setTextureUnitState(null);
  396.         }
  397.     }
  398.     }
  399.  
  400.  
  401.                
  402.     public static void main(String[] args) {
  403.         java.net.URL texURL1 = null;
  404.         java.net.URL texURL2 = null;
  405.         // the path to the image for an application
  406.         try {
  407.           texURL1 = new java.net.URL("file:../images/bg.jpg");
  408.           texURL2 = new java.net.URL("file:../images/one.jpg");
  409.         }
  410.         catch (java.net.MalformedURLException ex) {
  411.           System.out.println(ex.getMessage());
  412.           System.exit(1);
  413.         }
  414.  
  415.     Frame frame = new MainFrame(new InterleavedTest(texURL1, texURL2), 
  416.                     800, 800);
  417.     }
  418.                
  419.     public GeometryArray createGeometry (int type) {
  420.     GeometryArray tetra = null;
  421.         int texCoordSetMap[] = {0, 0};
  422.  
  423.     if (type == 1) {
  424.         tetra =new TriangleArray(12, 
  425.                      TriangleArray.COORDINATES|
  426.                      TriangleArray.COLOR_3|
  427.                      /*
  428.                        TriangleArray.NORMAL_3|
  429.                        */
  430.                      TriangleArray.TEXTURE_COORDINATE_2 |
  431.                      TriangleArray.INTERLEAVED|
  432.                      TriangleArray.BY_REFERENCE,
  433.                      2, texCoordSetMap);
  434.         
  435.         tetra.setInterleavedVertices(interleaved);
  436.  
  437.     }
  438.     else if (type == 2) {
  439.         tetra = new TriangleStripArray(12,
  440.                        TriangleStripArray.COORDINATES|
  441.                        TriangleStripArray.COLOR_3|
  442.                        /*
  443.                          TriangleArray.NORMAL_3|
  444.                          */
  445.                        TriangleArray.TEXTURE_COORDINATE_2 |
  446.                        TriangleStripArray.INTERLEAVED|
  447.                        TriangleStripArray.BY_REFERENCE,
  448.                        2, texCoordSetMap,
  449.                        stripVertexCounts);
  450.         tetra.setInterleavedVertices(interleaved);
  451.  
  452.         
  453.     }
  454.     else if (type == 3) { // Indexed Geometry
  455.         tetra = new IndexedTriangleArray(4,
  456.                          IndexedTriangleArray.COORDINATES|
  457.                          IndexedTriangleArray.COLOR_3|
  458.                          /*
  459.                            IndexedTriangleArray.NORMAL_3|
  460.                            */
  461.                          IndexedTriangleArray.TEXTURE_COORDINATE_2 |
  462.                          IndexedTriangleArray.INTERLEAVED|
  463.                          IndexedTriangleArray.BY_REFERENCE,
  464.                          2, texCoordSetMap,
  465.                          12);
  466.         tetra.setInterleavedVertices(indexedInterleaved);
  467.         ((IndexedTriangleArray)tetra).setCoordinateIndices(0, indices);
  468.         ((IndexedTriangleArray)tetra).setColorIndices(0, indices);
  469.         ((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
  470.                     0, 0, indices);
  471.         ((IndexedTriangleArray)tetra).setTextureCoordinateIndices(
  472.                     1, 0, indices);
  473.     }
  474.     else if (type == 4) { // Indexed strip geometry
  475.         tetra = new IndexedTriangleStripArray(4,
  476.                           IndexedTriangleStripArray.COORDINATES|
  477.                           IndexedTriangleStripArray.COLOR_3|
  478.                           /*
  479.                             IndexedTriangleArray.NORMAL_3|
  480.                             */
  481.                           IndexedTriangleArray.TEXTURE_COORDINATE_2 |
  482.                           IndexedTriangleStripArray.INTERLEAVED|
  483.                           IndexedTriangleStripArray.BY_REFERENCE,
  484.                               2, texCoordSetMap,
  485.                           12,
  486.                           stripVertexCounts);
  487.         tetra.setInterleavedVertices(indexedInterleaved);
  488.         ((IndexedTriangleStripArray)tetra).setCoordinateIndices(0, indices);
  489.         ((IndexedTriangleStripArray)tetra).setColorIndices(0, indices);
  490.         ((IndexedTriangleStripArray)tetra).setTextureCoordinateIndices(
  491.                     0, 0, indices);
  492.         ((IndexedTriangleStripArray)tetra).setTextureCoordinateIndices(
  493.                     1, 0, indices);
  494.     }
  495.     else if (type == 5) { // Interleaved array
  496.     }
  497.     return tetra;
  498.     }
  499. }
  500.